/**
 * 
 */
package com.shy.config;

import java.io.File;
import java.io.IOException;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import freemarker.cache.ClassTemplateLoader;
import freemarker.cache.FileTemplateLoader;
import freemarker.cache.MultiTemplateLoader;
import freemarker.cache.TemplateLoader;

/**
 * <p>Title:FreeMarkerConfig </p>
 * <p>Description: </p>
 * <p>Company:ipebg </p> 
 * @author H2013788
 * @date 2019年8月14日
 */
@Configuration
public class FreeMarkerConfig {

	 @Autowired
	 protected freemarker.template.Configuration configuration;
	 
	 
	 @Autowired
	 private FileUploadProperteis fileUploadProperteis;
	 
	 @PostConstruct
	 public void freeMarkerConfigurer() {
	 	configuration.setDefaultEncoding("UTF-8");  
	 	
	 	//全局处理空值，页面include必须用绝对路径了~~
	 	//configuration.setClassicCompatible(true);
		 
		// article_list即为页面上调用的标签名

		File templateDir = new File(fileUploadProperteis.getTemplateFolder());
		if(!templateDir.exists()) {
			templateDir.mkdirs();
		}
		// 绝对路径模板
		FileTemplateLoader fileTemplateLoader = null;
		try {
			fileTemplateLoader = new FileTemplateLoader(templateDir);
		} catch (IOException e) {
			e.printStackTrace();
		}

		// 本地模版加载
		ClassTemplateLoader classTemplateLoader = new ClassTemplateLoader(getClass().getClassLoader(), "templates");

		MultiTemplateLoader templateLoader = new MultiTemplateLoader(
				new TemplateLoader[] { classTemplateLoader, fileTemplateLoader });

		configuration.setTemplateLoader(templateLoader);
	 }
	
}
